// pages/admin-coupon/admin-coupon.js,
var util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hiddenmodalput: true,
    date: '点击选择-起始日期-',
    time: '点击选择-起始时间-',
    datetwo: '点击选择-截止日期-',
    timetwo: '点击选择-截止时间-',
    index: 0,
    navState: 0, //导航状态
  },
  //点击导航
  navSwitch: function (e) {
    // console.log(e.currentTarget.dataset.index)
    let index = e.currentTarget.dataset.index;
    this.setData({
      navState: index
    })
  },
  bindPickerChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  radiochange(e) {
    // console.log('radiochange', e.detail.value)
    this.setData({
      rule: e.detail.value
    })
  },

  bindTimeChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      time: e.detail.value
    })
  },
  bindDateChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
  bindTimeChangetwo: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      timetwo: e.detail.value
    })
  },
  bindDateChangetwo: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      datetwo: e.detail.value
    })
  },

  couponadd: function (e) {
    this.setData({
      hiddenmodalput: !this.data.hiddenmodalput
    })

  },
  cancelM: function (e) {
    this.setData({
      hiddenmodalput: true,
    })
  },
  confirmM: function (e) {
    console.log("优惠券的名称：" + this.data.name + "商品的类型：" + this.data.type + "  需要满足的价钱：" + this.data.priceone + "  优惠的价钱：" + this.data.pricetwo + "  优惠券的数量：" + this.data.number);
    console.log("起始日期：" + this.data.date + this.data.time + "截止日期：" + this.data.datetwo + this.data.timetwo)
    var startdate = this.data.date + " " + this.data.time
    var enddate = this.data.datetwo + " " + this.data.timetwo
    var key = this.data.arrxx[this.data.index].name;

    if (this.data.rule == '所有用户') {
      this.data.rule = '所有'
    } else if (this.data.rule == 'vip1') {
      this.data.rule = 'vip1'
    } else if (this.data.rule == 'vip2') {
      this.data.rule = 'vip2'
    } else if (this.data.rule == 'vip3') {
      console.log('vip3')
      this.data.rule = 'vip3'
    }

    var that = this;
    wx.request({
      url: 'http://localhost/shopping/admin_coupon_add.php',
      header: {
        'Content-Type': 'application/json'
      },
      data: {
        name: that.data.name,
        type: key,
        priceone: that.data.priceone,
        pricetwo: that.data.pricetwo,
        number: that.data.number,
        startdate: startdate,
        enddate: enddate,
        rule: that.data.rule,

      },
      success: function (res) {
        // console.log(res.data);
        console.log('添加优惠券成功！')
        that.Coupon();
        wx.showToast({
          title: '添加优惠券成功',
          icon: 'success',
          duration: 2000
        })
        that.setData({})
      },
      fail: function (res) {
        console.log("fail");
      }
    })
    this.setData({
      hiddenmodalput: true
    })
  },
  iName: function (e) {
    this.setData({
      name: e.detail.value,
    })
  },
  iPriceone: function (e) {
    this.setData({
      priceone: e.detail.value,
    })
  },
  iPricetwo: function (e) {
    this.setData({
      pricetwo: e.detail.value,
    })
  },
  iNumber: function (e) {
    this.setData({
      number: e.detail.value,
    })
  },
  coupondel: function (e) {
    var id = e.currentTarget.dataset.id
    console.log(id)
    let that = this;

    wx.showModal({
      title: '',
      content: '是否确认删除',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          wx.request({
            url: 'http://localhost/shopping/admin_coupon_del.php',
            header: {
              'Content-Type': 'application/json'
            },
            data: {
              id: id,
            },
            success: function (res) {
              // console.log(res.data);
              console.log('删除优惠券成功！')
              that.Coupon();
              that.setData({})
            },
            fail: function (res) {
              console.log("fail");
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })


    this.setData({})
  },
  couponextend(e) {
    var id = e.currentTarget.dataset.id
    var extend = e.currentTarget.dataset.extend
    var time = util.formatTime(new Date());
    console.log(id, extend, time)
    if (extend == "未领取") {
      wx.showModal({
        title: '',
        content: '您已经发放了',
      })
      console.log("该优惠券您已发放，请选择其他")
    } else if (extend == "未发放") {
      let that = this;

      wx.request({
        url: 'http://localhost/shopping/admin_coupon_edit.php',
        header: {
          'Content-Type': 'application/json'
        },
        data: {
          id: id,
          enddate: time,
          // extend:extend,
        },
        success: function (res) {
          // console.log(res.data);
          console.log('发放优惠券成功！')
          that.Coupon();
          wx.showToast({
            title: '成功发放优惠券',
            icon: 'success',
            duration: 2000
          })
          that.setData({})
        },
        fail: function (res) {
          console.log("fail");
        }
      })
    }
  },
  Coupon: function () {
    var that = this;
    wx.request({
      url: 'http://localhost/shopping/admin_coupon.php',
      method: 'GET',
      header: {
        'Content-Type': 'application/json'
      },
      data: {},
      success: function (res) {
        console.log(res.data)
        that.setData({
          arr: res.data.coupon,

        })
      },
      fail: function (res) {
        console.log("fail");
      }
    })
    wx.request({
      url: 'http://localhost/shopping/admin_type.php',
      header: {
        'Content-Type': 'application/json'
      },
      data: {},
      success: function (res) {
        console.log(res.data);
        that.setData({
          arrxx: res.data.type,
        })
      },
      fail: function (res) {
        console.log("fail");
      }
    })
    wx.request({
      url: 'http://localhost/shopping/coupon.php',
      header: {
        'Content-Type': 'application/json'
      },
      data: {},
      success: function (res) {
        console.log(res.data);
        that.setData({
          arrcoupon: res.data.coupon,
        })
      },
      fail: function (res) {
        console.log("fail");
      }
    })
    wx.request({
      url: 'http://localhost/shopping/admin_coupon_search.php',
      header: {
        'Content-Type': 'application/json'
      },
      data: {},
      success: function (res) {
        console.log(res.data);
        that.setData({
          arrcoupons: res.data.coupons,
        })
      },
      fail: function (res) {
        console.log("fail");
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.Coupon();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})